<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<style>
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }


    .container{
        width: 500px;
        height: 300px;
        margin: 10px auto;
        position: relative;
        overflow: hidden;

    }

    .carousel{
        width: 100%;
        height: 100%;
        display: flex;
        transition: all 0.5s;
        transform: translateX(-300%);
    }

    .carousel .item{
        width: 100%;
        height: 100%;
        flex-shrink: 0;
        background-color: lightseagreen;

    }
    .carousel .item:nth-child(2){
        background-color: orange;
    }
    .carousel .item:nth-child(3){
        background-color: darkgoldenrod;
    }
    .carousel .item:nth-child(4){
        background-color: darkgrey;
    }

    .indicator{
        position: absolute;
        bottom: 10px;
        left: 50%;
        transform: translateX(-50%);
    }


    .indicator span{
        width: 20px;
        height: 20px;
        border: 2px solid red;
        display: inline-block;
        border-radius: 50%;
        margin: 0 3px;
    }

    .indicator span.active{
        background-color: yellow;
        border-color: yellow;
    }
</style>

<div class="container">

    <div class="carousel">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
    </div>


    <div class="indicator">
        <span class="active"></span>
        <span></span>
        <span></span>
        <span></span>

    </div>
</div>


<script>
    var carousel = document.querySelector(".carousel");
    var indicators = document.querySelectorAll(".indicator span");


    /**
     * 移动到第几个板块
     * @param index
     */
    function moveTo(index){
        carousel.style.transform = `translateX(-${index}00%)`


        // 设置指示器
        var active = document.querySelector(".indicator span.active");
        active.classList.remove("active")

        indicators[index].classList.add("active")
    }



    indicators.forEach(function (item,index){
        item.onclick = function (){
            moveTo(index)
        }
    })
</script>
</body>
</html>
